package ru.xpparty.scanner;

/**
 * POJO(plain old java object) for range of ports
 * 
 * @author xp.party
 *
 */
public class PortRange {
    private int start;
    private int end;

    /**
     * 
     * @return start of port range
     */
    public int getStart() {
        return start;
    }

    /**
     * 
     * @return end of port range
     */
    public int getEnd() {
        return end;
    }
    
    /**
     * Constructs new port range with start port and end port
     * 
     * @param start port
     * @param end port
     */
    public PortRange(int start, int end) {
        checkPort(start);
        checkPort(end);
        chechPortOrder(start, end);
        this.start = start;
        this.end = end;
    }
    
    /**
     * Constructs port range with one port
     * 
     * @param port
     */
    public PortRange(int port) {
        checkPort(port);
        start = port;
        end = port;
    }

    /**
     * Performs validation of given port
     * 
     * @param port given port
     * @throws InvalidPortException if port out of range (0..65535)
     */
    private void checkPort(int port) {
        if (isValid(port)) {
            throw new InvalidPortException();
        }
    }
    
    /**
     * Check order of given ports (start must be lesser or equivalent to end)
     * 
     * @param start port
     * @param end port
     * @throws InvalidPortException if order is invalid
     */
    private void chechPortOrder(int start, int end) {
		 if (start > end) {
			 throw new InvalidPortException();
		 }
	}       

    /**
     * Returns true if port in range(0..65535)
     * 
     * @param port given port
     * 
     * @return true if port in range(0..65535)
     */
    private boolean isValid(int port) {
        return (port < 0 || port > 65535);
    }   
}
